import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/my_device_list_model.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:core_package/cache/user_cache.dart';
import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/province_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/basice/my_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_sex_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_title_and_detail_widget.dart';
import 'package:flutter_app_unionad/ui/basice/show_select_address_dialog.dart';

class ChangeUserDeviceInfoPage extends StatefulWidget {
  final MyDeviceListModel model;
  static const String routeName = "/changeUserDeviceInfoPage";

  const ChangeUserDeviceInfoPage(this.model, {Key? key}) : super(key: key);

  @override
  State<ChangeUserDeviceInfoPage> createState() => _ChangeUserDeviceInfoPageState();
}

class _ChangeUserDeviceInfoPageState extends State<ChangeUserDeviceInfoPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    name = widget.model.contractName;
    phone = widget.model.contractTel!.replaceRange(3, 7, " **** ");
    address = widget.model.devAddress;
    _provinceModel = ProvinceModel(id: int.parse(widget.model.devProvinceCode!), name: widget.model.devProvince);
    _cityModel = ProvinceModel(id: int.parse(widget.model.devCityCode!), name: widget.model.devCity);
    _areaModel = ProvinceModel(id: int.parse(widget.model.devCountyCode!), name: widget.model.devCounty);
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
      title: '修改',
      child: buildContent(),
      needScrollView: true,
    );
  }

  String? name;
  String? phone;
  String? address;
  ProvinceModel? _provinceModel;
  ProvinceModel? _cityModel;
  ProvinceModel? _areaModel;

  Widget buildContent() {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: 40.px,
            vertical: 40.px,
          ),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(5.px),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InputSeparatorWidget(
                title: "姓名",
                placeholder: "请输入姓名",
                valueChange: (value) => name = value,
                controller: TextEditingController(text: name),
              ),
              MySizeBox(height: 10.px),
              InputSeparatorWidget(
                title: "联系电话",
                placeholder: "请输入联系电话",
                valueChange: (value) => phone = value,
                controller: TextEditingController(text: phone),
                maxLength: 11,
                keyboard: TextInputType.number,
              ),
              MySizeBox(height: 10.px),
              SelectTitleAndDetailWidget(
                  title: "选择区域",
                  detail:
                      _provinceModel != null ? _provinceModel!.name! + _cityModel!.name! + _areaModel!.name! : "请选择区域",
                  icon: Icon(Icons.arrow_drop_down, size: 15.px),
                  onTap: () => selectCity()),
              MySizeBox(height: 10.px),
              InputSeparatorWidget(
                title: "详细地址",
                placeholder: "请输入详细地址",
                valueChange: (value) => address = value,
                controller: TextEditingController(text: address),
              ),
            ],
          ),
        ),
        MySizeBox(height: 40.px),
        Center(
          child: MyButton(
            title: "保存",
            width: 200.px,
            onTap: () {
              //
              saveInfo();
            },
          ),
        ),
      ],
    );
  }

  void selectCity() {
    ShowSelectAddressDialog.showAddress(context, onSelected: (province, city, area) {
      _provinceModel = province;
      _cityModel = city;
      _areaModel = area;
      setState(() {});
    });
  }

  void saveInfo() {
    if (name == null || name!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "姓名不能为空");
      return;
    }


    if (phone == null || phone!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "联系电话不能为空");
      return;
    }
    if(phone!.contains("**")){
      phone = widget.model.contractTel;
    }
    if (!isPhoneString(phone)) {
      ShowToastWidget.showToast(msg: "联系电话格式不对");
      return;
    }
    if (_provinceModel == null) {
      ShowToastWidget.showToast(msg: "请选择区域");
      return;
    }
    if (address == null || address!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "详细地址不能为空");
      return;
    }
    ProgressDialog.showProgress(context);
    FacilityRequest.changeDeviceInfo(
      devId: widget.model.devId!,
      userAddress: address,
      userTel: phone,
      userName: name,
      city: _cityModel!.name,
      userCityCode: _cityModel!.id.toString(),
      userProvince: _provinceModel!.name,
      userProvinceCode: _provinceModel!.id.toString(),
      userCounty: _areaModel!.name,
      userCountyCode: _areaModel!.id.toString(),
    ).then((value) {
      ProgressDialog.dismiss(context);
      if (value) {
        UserCacheUil.setBool(Configuration.makeAnInstall, true);

        Navigator.pop(context, true);
      }
    });
  }
}
